﻿using GoViewServer.Application.Entities;
using System.Text;

namespace GoViewServer.Application.Services
{
    [DynamicApiController]
    public class TestService
    {
        ISqlSugarClient _db;
        public TestService(ISqlSugarClient db)
        {
            _db = db;
        }

        [HttpGet]
        public dynamic T1()
        {
            var res = new
            {
                dimensions = new List<string>() { "product", "data1", "data2" },
                source = new List<Test>() { 
                    new Test(){ product="Mon",data1=  Random(),data2=Random()},
                    new Test(){ product="Tue",data1=Random(),data2=Random()},
                    new Test(){ product="Wed",data1=Random(),data2=Random()},
                    new Test(){ product="Thu",data1=Random(),data2=Random()},
                    new Test(){ product="Fri",data1=Random(),data2=Random()},
                    new Test(){ product="Sta",data1=Random(),data2=Random()},
                    new Test(){ product="Sun",data1=Random(),data2=Random()},
                }
            };
            return res;
    }
        [HttpPost("/api/test/t2/{type}")]
        public dynamic T2(string type,SQLDto data) {
            var a = 1;
            Random rd = new Random();
            return rd.Next(100) ;
        }

        

        [HttpGet]
        public dynamic T3()
        {
            var res = new List<dynamic>();
            for (int i = 0; i < 10; i++)
            {
                var list = new List<dynamic>() {
                    Random(),Random(),Random(),Random(),Random()
                };
                res.Add(list);
            }
            return res;
        }

        [HttpGet]
        public dynamic ErrorTest()
        {
            throw Oops.Oh("异常测试");
        }

        [HttpPost("execSql")]
        public   async Task<dynamic> ExecSql(SQLDto data)
        {
            var _pmsDb = _db.AsTenant().GetConnection("pms");
            var res=await _pmsDb.SqlQueryable<dynamic>(data.sql).ToListAsync();
            return res;
        }

        [HttpGet]
        public void CreateDb()
        {
            _db.CodeFirst.InitTables(typeof(Task));
        }

        private int Random() {
            return new Random().Next(111, 9999);
        }

        public class SQLDto
        {
            public string sql { get; set; }
        }

    public class Test {
        public string product { get; set; }
        public int data1 { get; set; }
        public int data2 { get; set; }
    }

        public class Task
        {
            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
            public int Id { get; set; }

            /// <summary>
            /// 任务名称
            /// </summary>
            public string TaskName { get; set; } = string.Empty;

            /// <summary>
            /// 项目编号
            /// </summary>
            public string? ProjectCode { get; set; }

            /// <summary>
            /// 分组名称
            /// </summary>
            public string? GroupName { get; set; }

            /// <summary>
            /// 任务的开始时间
            /// </summary>
            public DateTime BeginDate { get; set; }

            /// <summary>
            /// 任务的结束时间
            /// </summary>
            public DateTime EndDate { get; set; }

            /// <summary>
            /// 任务创建时间
            /// </summary>
            public DateTime CreatedDate { get; set; }

            /// <summary>
            /// 任务创建人id
            /// </summary>
            public int CreatedUserId { get; set; }

            /// <summary>
            /// 更改时间
            /// </summary>
            public DateTime ModifyDate { get; set; }

            /// <summary>
            /// 任务状态
            /// </summary>
            public TaskStatusEnum Status { get; set; }

            /// <summary>
            /// 任务等级
            /// </summary>
            public TaskLevelEnum Level { get; set; }

            /// <summary>
            /// 任务备注
            /// </summary>
            public string? Remark { get; set; }

            /// <summary>
            /// 任务进度
            /// </summary>
            public double Progress { get; set; }

            /// <summary>
            /// 配置任务填写的formId
            /// </summary>
            public int FormId { get; set; }

            /// <summary>
            /// 删除任务
            /// </summary>
            public bool Delete { get; set; } = false;
        }

        public enum TaskLevelEnum : int
        {
            A = 1,
            B = 2,
            C = 3,
            D = 4
        }

        public enum TaskStatusEnum : int
        {
            /// <summary>
            /// 草稿
            /// </summary>
            Draft = 0,
            /// <summary>
            /// 待执行
            /// </summary>
            Waiting = 1,
            /// <summary>
            /// 执行中
            /// </summary>
            Execution = 2,
            /// <summary>
            /// 完成
            /// </summary>
            Complete = 3
        }

    }
}